package decision;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import lifeDomain.State;
import support.Variables;
import android.os.Environment;
import android.util.Log;

public class DTree {
	public TData raw;	
	
	public DTree()
	{
		
	}
	public void PutData(ArrayList<State[]> states)
	{
		raw = new TData(states);
	}
	public void PutData(TData _raw)
	{
		raw = _raw;
	}
	public void Run()
	{
		
	}
	public void Run(State trigger)
	{
		
	}
	public void PrintRule()
	{
	}
	
	
	public class Tree<TT>
	{
		
		
		
		public TagNode<TT> head;
		public Node<TT> cursor;
		
		
		public Tree()
		{
			this.head = new TagNode<TT>();
		}
		
		public StringBuilder PrintDFS()
		{
			StringBuilder sb = new StringBuilder();
			
			head.PrintDFS(sb,0);
			Log.d(Variables.tag, sb.toString());
			return sb;
		}
		
		public void CursorToHead()
		{
			cursor  = head;
		}
		public void SaveTreeToFile(String seq)
		{
			String backupPath  = "/sense";
	    	File sd = new File(Environment.getExternalStorageDirectory()+backupPath);

	            
	        String fileName = "rule" + seq;
	            
	        try{
	            if(!sd.exists())
	            {
	            	sd.mkdirs();
	            }
	            File ruleFile = new File(sd, fileName);
	            if(!ruleFile.exists())
	            {
	            	ruleFile.createNewFile();
	            }
	        	// Do something for file here
	            FileWriter fw =  new FileWriter(ruleFile);
	            DFSPrint(fw, 0, head);
	            fw.close();
	        }catch(IOException ee)
	        {
	        	ee.printStackTrace();
	        }

		}
		
		public void DFSPrint(FileWriter fw, int level, TagNode<TT> node)
		{
			int i;
			if(node.data != null){
				StringBuilder sb = new StringBuilder("\t");
				for(i=0;i < level;i++)
				{
					sb.append("-");
				}
				sb.append("<<"+node.mTag + " " + node.data.toString()+">>");
				try
				{
					fw.write(sb.toString());
				}
				catch(Exception ee)
				{
					ee.printStackTrace();
				}
			}
			if(node.next == null)
				return;
			for(i=0;i<node.next.size();i++)
			{
				DFSPrint(fw, level+1, node.next.get(i));
			}
		}
		
	}
	
	
	
	public class TagNode<T> extends Node<T>
	{
		ArrayList<TagNode<T>> next;
		public static final int DOIT = 1;
		public static final int DONTDOIT = 2;
		int mTag;
		int mDecision = DONTDOIT;
		int positive;
		int negative;
		public TagNode()
		{
			next = new ArrayList<TagNode<T>>();
		}
		public void Add(int _tag,T d)
		{
			super.Add(d);
			mTag = _tag;
			
		}
		public void Add(int _tag,T d, int pos, int neg)
		{
			super.Add(d);
			mTag = _tag;
			positive = pos;
			negative = neg;
		}
		public void AddChild(TagNode<T> n)
		{
			next.add(n);
		}
		public int GetDicision()
		{
			return mDecision; 
		}
		public boolean isDicisionNode()
		{
			return data == null ? true: false;
		}
		public void SetDicision(int d)
		{
			mDecision = d;
		}
		
		public void RemoveChild(Node<T> n)
		{
			if(next.contains(n))
			{
				next.remove(n);
			}
		}
		public void PrintDFS(StringBuilder sb,int level)
		{
			
			String gap="";
			String handle="";
			for(int i=0;i<level;i++)
			{
				gap += "\t";
				handle += "-";
			}
			if(data != null){	
				sb.append(gap + handle + data.toString() +
						" pos neg<" + positive + ", " + negative + ">" );
				
			}
			if(this.next == null)
				return;
			for(int i = 0; i< next.size();i++)
			{
				next.get(i).PrintDFS(sb, level + 1);
				sb.append("\n");
			}
		}
	}
	public class Node<T>
	{
		T data;
		ArrayList<Node<T>> next;
		public Node()
		{
			data = null;
			next = new ArrayList<Node<T>>();
		}
		public void Add(T d)
		{
			data = d;
		}
		public void AddChild(Node<T> n)
		{
			next.add(n);
		}
		public void RemoveChild(Node<T> n)
		{
			if(next.contains(n))
			{
				next.remove(n);
			}
		}
		public void PrintDFS(StringBuilder sb,int level)
		{
			
			String gap="";
			String handle="";
			for(int i=0;i<level;i++)
			{
				gap += "\t";
				handle += "-";
			}
			if(data != null){	
				sb.append(gap + handle + data.toString() );
				
			}
			for(int i = 0; i< next.size();i++)
			{
				next.get(i).PrintDFS(sb, level + 1);
				sb.append("\n");
			}
		}
		
	}
	 
}
